package net.brucecooper.mindmap.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class OptionDialog extends DialogBox {
    public int selectedOption = -1;

    public OptionDialog(String title, Widget w, String[] options, OptionSelectionHandler handler) {
        super(false, true);

        HorizontalPanel buttons = new HorizontalPanel();
        for (int i = 0; i < options.length; i++) {
            Button uploadButton = new Button(options[i]);
            buttons.add(uploadButton);
            uploadButton.addClickHandler(new OptionClickHandler(i, handler));
        }

        VerticalPanel p = new VerticalPanel();

        p.add(w);
        p.add(buttons);
        add(p);
        setHTML("<strong>" + title + "</strong>");
//        setPopupPosition((MindMap.this.getOffsetWidth() - 200) / 2, 50);
    }
    
    public int getSelectedOption() {
        return selectedOption;
    }
    
    private class OptionClickHandler implements ClickHandler {
        int option;
        private OptionSelectionHandler handler;
        
        public OptionClickHandler(int option, OptionSelectionHandler handler) {
            this.option = option;
            this.handler = handler;

        }
        
        public void onClick(ClickEvent event) {
            selectedOption = option;
            if (handler.optionSelected(option)) {
                hide();
            }
        }
    }
}
